package kr.co.medical.controller;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import kr.co.medical.dao.MedicalDao;
import kr.co.medical.dao.OfficeDao;
import kr.co.medical.dao.PatientDao;
import kr.co.medical.dto.CalBean;
import kr.co.medical.dto.Cheobang_All;
import kr.co.medical.dto.Cheobang_Jupsoo;
import kr.co.medical.dto.Cheobangjeon;
import kr.co.medical.dto.Dept;
import kr.co.medical.dto.Disease;
import kr.co.medical.dto.Doctor;
import kr.co.medical.dto.DoctorMajor;
import kr.co.medical.dto.DoctorSchedule;
import kr.co.medical.dto.Jupsoo;
import kr.co.medical.dto.Medicine_Cheobang_Patient;
import kr.co.medical.dto.NoSoonap;
import kr.co.medical.dto.No_Soonap_Check;
import kr.co.medical.dto.Patient;
import kr.co.medical.dto.Soonap;
import kr.co.medical.dto.Ward;
import kr.co.medical.dto.WardMaster;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

@Controller
public class OfficeController {
	@Autowired
	private SqlSession sqlSessionTemplate;
	
	@RequestMapping("saghaejindan.do")
	public String sanghae(){
		return "office.sanghaejindanseo";
	}
	@RequestMapping("cheobangjeon.do")
	public ModelAndView cheobangjeon(String cheobang_id,ModelAndView mav) throws ClassNotFoundException, SQLException{
		if(cheobang_id.equals("")){
			mav.setViewName("redirect:/cheobang.do");
			return mav;
		}
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		List<Cheobangjeon> cheobangjeon = new ArrayList<Cheobangjeon>();
		cheobangjeon = officedao.select_medicine(cheobang_id);
		Cheobang_All cheobangall = new Cheobang_All();
		cheobangall = officedao.cheobang_all3(cheobang_id);
		Patient patient = officedao.cheobang_patient(cheobang_id);
		List<Disease> disease = new ArrayList<Disease>();
		disease = officedao.disease_select(cheobang_id);
		mav.addObject("cheobangall", cheobangall);
		mav.addObject("mediclist", cheobangjeon);
		mav.addObject("patient", patient);
		mav.setViewName("office.cheobangjeon");
		mav.addObject("diseaselist", disease);
		return mav;
	}
	@RequestMapping("wardcheobang.do")
	public ModelAndView wardcheobang(ModelAndView mav) throws ClassNotFoundException, SQLException {
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		List<NoSoonap> wardlist = new ArrayList<NoSoonap>();
		wardlist = officedao.wardcheobanglist();
		mav.addObject("wardlist", wardlist);
		mav.setViewName("office.wardcheobang");
		return mav;
	}
	
	@RequestMapping("office.do")
	public String join(){
		return "office.officemain";
	}
	
	@RequestMapping("redirect.do")
	public String redirect(){
		return "redirect:/index.do";
	}
	
	@RequestMapping("patientinfo.do")
	public ModelAndView patientinfo(ModelAndView mav) throws ClassNotFoundException, SQLException{
		PatientDao patientdao = sqlSessionTemplate.getMapper(PatientDao.class);
		mav.setViewName("office.patientinfo");
		//mav.addObject("office",master.getId());
		List<Patient> list = new ArrayList<Patient>();
		list = patientdao.selectPatient();
		mav.addObject("list", list);
		return mav;
		
	}
	
	
	@RequestMapping("namecheck.do")
	public ModelAndView NameCheck(String name) throws ClassNotFoundException, SQLException{
		PatientDao patientdao = sqlSessionTemplate.getMapper(PatientDao.class);
		System.out.println(name);
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		List<Patient> list = new ArrayList<Patient>();
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("name", name);
		list = patientdao.NameCheck(map); 
		ArrayList<Object> p_name = new ArrayList<Object>();
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i).getPatient_id());
			p_name.add(list.get(i).getName());
		}
		mav.addObject("list", list);
		mav.addObject("name", p_name);
		return mav;
	}
	
	
	
	@RequestMapping(value={"patientinfojoin.do"},method=RequestMethod.POST)
	public ModelAndView infojoin(Patient patient,ModelAndView mav) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		officedao.infoinsert(patient);
		mav.setViewName("redirect:/patientinfo.do");
		
		return mav;
	}
	
	@RequestMapping("patientdetail.do")
	@ResponseBody
	public String patientdetail(String name) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		Patient patient=officedao.selectpatient(name);

		return "office.roominfo";
	}
	
	@RequestMapping("showinfo.do")
	public ModelAndView showofficeinfo(String name , String jumin1 ,String patient_id, HttpSession session) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		session.setAttribute("patient_id", patient_id);
		Patient patient=officedao.selectofficeinfo(name, jumin1);
		
		List<Cheobang_All> list = officedao.cheobang_all2(patient_id);
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		
		mav.addObject("list",list);
		mav.addObject("patient",patient);
		return mav;
	}
	
	@RequestMapping("roominfo.do")
	public String roominfo(HttpSession session){
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		String patient_id=(String)session.getAttribute("patient_id");
		//officedao.
		
		return "office.roominfo";
	}
	
	
	@RequestMapping("roomselect.do")
	public ModelAndView roomselect(HttpSession session,Jupsoo jupsoo,ModelAndView mav) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		String patient_id=(String)session.getAttribute("patient_id");
		Patient patient=officedao.jubsoopatient(patient_id);
		//HashMap<String,Object> m = new HashMap<String, Object>();
		//m.put("is_night", is_night);
		//m.put("is_holiday", is_holiday);
		//m.put("jupsoo_code_id", jupsoo_code_id);
		//m.put("bohum_code", bohum_code);
		//mav.addObject("m",m);
		mav.addObject("p",patient);
		
		mav.addObject("jupsoo",jupsoo);
		
		mav.setViewName("office.roominsert");
		
		return mav;
	}
	
	
	
	@RequestMapping("wardinsert.do")
	@Transactional
	@ResponseBody
	public String roominsert(HttpSession session,Jupsoo jupsoo,String bedNum,String wardNum) throws ClassNotFoundException, SQLException{
		int ward_Num = Integer.parseInt(wardNum);
		int bed_Num = Integer.parseInt(bedNum);
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		String patient_id=(String)session.getAttribute("patient_id");
		List<WardMaster> wm = new ArrayList<WardMaster>(); 
		wm = (List<WardMaster>)officedao.wardPatientInfo(ward_Num, bed_Num);
		Jupsoo jupsoo2 =new Jupsoo();
		if(wm.size() == 0){
			officedao.addreser_v(jupsoo);
			officedao.addreser_j2(jupsoo);
			jupsoo2=officedao.jupsoo_id(patient_id);
			String jupsoo_id=jupsoo2.getJupsoo_id();
			officedao.wardjupsoo(ward_Num, bed_Num, patient_id,jupsoo_id);
			int i=officedao.wardjupsoo_his(ward_Num, bed_Num, patient_id,jupsoo_id);
			officedao.wsoonapinsert(jupsoo_id);
			return "yes";
		}else{
			return "no";
		}
	}
	
	
	
	//처방 시작
	@RequestMapping("cheobang.do")
	public ModelAndView cheobang(ModelAndView mav) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		List<Cheobang_Jupsoo> list = new ArrayList<Cheobang_Jupsoo>();
		list= officedao.cheobanglist();
		mav.setViewName("office.cheobang");
		mav.addObject("list",list);
		return mav;
	}
	
	//처방 사이드 리스트 비동기
	@RequestMapping("cheobangcheck.do")
	public ModelAndView CheobangCheck(String NAME) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		List<Cheobang_Jupsoo> list = new ArrayList<Cheobang_Jupsoo>();
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("name", NAME);
		list = officedao.cheobangcheck(map);
		mav.addObject("list", list);
		return mav;
	}
	@RequestMapping("wardsoonap.do")
	public ModelAndView wardsoonapcheck(String NAME) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		System.out.println(NAME);
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		List<NoSoonap> list = new ArrayList<NoSoonap>();
		String name = NAME;
		list = officedao.wardcheobangcheck3(name);
		mav.addObject("list", list);
		return mav;
	}
	
	@RequestMapping("wardsoonapfinal.do")
	public ModelAndView wardsoonapfinal(String jupsoo_id)  throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		System.out.println(jupsoo_id);
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		NoSoonap nosoonap = officedao.wardnosoonap(jupsoo_id);
		Patient patient = officedao.selectpatient2(jupsoo_id);
		mav.addObject("nosoonap", nosoonap);
		mav.addObject("patient", patient);
		return mav;
	}
	
	@RequestMapping("cheobangcheck2.do")
	public ModelAndView CheobangCheck2(String NAME) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		System.out.println(NAME);
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		List<Cheobang_Jupsoo> list = new ArrayList<Cheobang_Jupsoo>();
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("name", NAME);
		list = officedao.cheobangcheck2(map);
		for(int i =0;i<list.size();i++){
			System.out.println(list.get(i).getNAME());
		}
		mav.addObject("list", list);
		return mav;
	}
	@RequestMapping("wardcheobangcheck2.do")
	public ModelAndView wardCheobangCheck2(String NAME) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		System.out.println(NAME);
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		List<NoSoonap> list = new ArrayList<NoSoonap>();
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("name", NAME);
		list = officedao.wardcheobangcheck2(map);
		for(int i =0;i<list.size();i++){
			System.out.println(list.get(i).getName());
		}
		mav.addObject("list", list);
		return mav;
	}
	
	
	//처방 사이드 리스트 비동기
	@RequestMapping("cheobanginfo.do")
	public ModelAndView cheobanginfo(String NAME , String JUPSOO_ID ,String CHEOBANG_ID) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		Patient patient=officedao.selectpatinfo(NAME);
		List<Cheobang_All> ch = officedao.cheobang_all(JUPSOO_ID, CHEOBANG_ID);
		List<Medicine_Cheobang_Patient> list=officedao.medic_cheobang_p(CHEOBANG_ID);
		List<Medicine_Cheobang_Patient> doselist=officedao.dose(CHEOBANG_ID);
		Soonap soonap=officedao.selectsoonap(JUPSOO_ID, CHEOBANG_ID);
		Soonap nosoonap=officedao.selectno_soonap(CHEOBANG_ID);
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("patient",patient);
		mav.addObject("ch",ch);
		mav.addObject("mediclist",list);
		mav.addObject("doselist",doselist);
		mav.addObject("soo",soonap);
		mav.addObject("nosoonap",nosoonap);
		return mav;
	}
	

	@RequestMapping(value="nosoonapsubmit.do")
	public ModelAndView nosoonap(String cheobang_id,int no_soonap_btn,ModelAndView mav) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		No_Soonap_Check no=officedao.no_soonap_check(cheobang_id);
		if(no_soonap_btn >=no.getRemainder_price()){
			officedao.soonap_completion(no);
			officedao.delete_mid(no);
			officedao.delete_no_soonap(no);
		}else if(no_soonap_btn < no.getRemainder_price()){
			no.setMid_price(no_soonap_btn);
			officedao.mid_insert(no);
			no.setRemainder_price(no.getRemainder_price()-no_soonap_btn);
			officedao.update_nosoonap(no);
			
		}
		mav.setViewName("redirect:/cheobang.do");
		return mav;
	}
	@RequestMapping(value="wardnosoonapsubmit.do")
	@Transactional
	public ModelAndView wardnosoonapfinal(String jupsoo_id, int no_soonap_btn, ModelAndView mav) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		No_Soonap_Check no = officedao.wardnosoonap2(jupsoo_id);
		if(no_soonap_btn >=no.getRemainder_price()){
			officedao.soonap_completion(no);
			officedao.delete_mid(no);
			officedao.delete_no_soonap(no);
		}else if(no_soonap_btn < no.getRemainder_price()){
			no.setMid_price(no_soonap_btn);
			officedao.mid_insert(no);
			no.setRemainder_price(no.getRemainder_price()-no_soonap_btn);
			officedao.update_nosoonap(no);
			
		}
		
		
		mav.setViewName("redirect:/cheobang.do");
		return mav;
	}

	@RequestMapping("showsoonapinfo.do")
	@ResponseBody
	public HashMap<String, Object> shownosoonap(String patient_id) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		List<NoSoonap> nosoonaplist = new ArrayList<NoSoonap>();
		nosoonaplist = officedao.shownosoonapinfo(patient_id);
		String totalnosoonap = officedao.total_nosoonap(patient_id);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("nosoonap", nosoonaplist);
		map.put("total", totalnosoonap);
		return map;
	}
	
	@RequestMapping("showsoonapinfo2.do")
	@ResponseBody
	public HashMap<String, Object> shownosoonap2(String patient_id) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		List<NoSoonap> soonaplist = new ArrayList<NoSoonap>();
		soonaplist = officedao.showsoonapinfo(patient_id);
		HashMap<String, Object> map = new HashMap<String, Object>();
		System.out.println(soonaplist.get(0).getCheobang_id());
		for(int i=0;i<soonaplist.size();i++){
			if(soonaplist.get(i).getCheobang_id() == null){
				soonaplist.get(i).setCheobang_id("입원");
			}
		}
		map.put("soonap", soonaplist);
		return map;
	}
	
	@RequestMapping(value="midsoonapsubmit.do")
	public ModelAndView midsoonap(String cheobang_id,String no_soonap_btn,ModelAndView mav){
		System.out.println("mid");
		System.out.println(cheobang_id);
		System.out.println(no_soonap_btn);
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		mav.setViewName("office.cheobang");
		return mav;
	}
	
	@RequestMapping("jindanjupsoo.do")
	public ModelAndView jindanjupsoo(String jupsoo_id,ModelAndView mav,String patient_id) throws ClassNotFoundException, SQLException{
		System.out.println(jupsoo_id);
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		Jupsoo jupsoo = officedao.jupsoo_id(patient_id);
		mav.addObject("jupsoo_id", jupsoo_id);
		mav.addObject("name", jupsoo.getName());
		mav.setViewName("office.jindanjupsoo");
		return mav;
	}
	 
	@RequestMapping("jupsoo.do")
	public ModelAndView jupsoo(HttpSession session, ModelAndView mav) throws ClassNotFoundException, SQLException{
		if(session.getAttribute("patient_id") == null){
			mav.setViewName("redirect:/patientinfo.do");
			return mav;
		}
		String patient_id = (String) session.getAttribute("patient_id");
		//System.out.println(patient_id);
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		Patient patient=officedao.jubsoopatient(patient_id);
		List<Dept> list = new ArrayList<Dept>();
		list = officedao.selectDept();
		mav.addObject("deptlist", list);
		mav.addObject("patient", patient);
		mav.addObject("patient_id", patient_id);
		mav.setViewName("office.jupsoo");
		String currentdate = officedao.currentdate();
		mav.addObject("currentdate", currentdate);
		return mav;
	}
	
	@RequestMapping("o_schedule.do")
	public ModelAndView cal(ModelAndView mav) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		List<Doctor> list = new ArrayList<Doctor>();
		list = officedao.showdoctor();
		mav.addObject("doctors", list);
		mav.setViewName("office.o_schedule");
		return mav;
	}
	
	
	@RequestMapping("office_reser.do")
	public ModelAndView o_reservation(ModelAndView mav, HttpSession session) throws ClassNotFoundException, SQLException{
		if(session.getAttribute("patient_id") == null){
			mav.setViewName("redirect:/patientinfo.do");
			return mav;
		}
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		List<Dept> list = new ArrayList<Dept>();
		list = officedao.selectDept();
		String patient_id = (String) session.getAttribute("patient_id");
		Patient patient=officedao.jubsoopatient(patient_id);
		mav.addObject("deptlist", list);
		mav.addObject("patient", patient);
		mav.addObject("patient_id", patient_id);
		mav.setViewName("office.office_reser");
		return mav;
	}
	@RequestMapping("soonaplist.do")
	public ModelAndView soonaplist(ModelAndView mav,HttpSession session) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		String patient_id = (String) session.getAttribute("patient_id");
		Patient patient=officedao.jubsoopatient(patient_id);
		
		mav.addObject("patient", patient);
		mav.setViewName("office.soonaplist");
		
		return mav;
	}
	@RequestMapping("soonap.do")
	public ModelAndView soonap(ModelAndView mav, HttpSession session) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		String patient_id = (String) session.getAttribute("patient_id");
		Patient patient=officedao.jubsoopatient(patient_id);
		
		mav.addObject("patient", patient);
		mav.setViewName("office.soonap");
		return mav;
	}

	@RequestMapping(value={"selectdoctor.do"}, produces="text/plain;charset=UTF-8")
	@ResponseBody
	public HashMap<String, Object> selectdoctor(String dept_id) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		System.out.println(dept_id);
		List<DoctorMajor> list = new ArrayList<DoctorMajor>();
		list=officedao.selectDoctor(dept_id);
		HashMap<String, Object> mapJson = new HashMap<String, Object>();
		System.out.println(list.get(0).getDoctor_id());
		mapJson.put("doctorlist",list); 
		System.out.println(mapJson.get("doctorlist"));
		return mapJson;
	}
	
	@RequestMapping(value={"showschedule.do"}, produces="text/plain;charset=UTF-8")
	public  ModelAndView showschedule(String doctor_id) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		System.out.println(doctor_id);
		List<DoctorSchedule> list = new ArrayList<DoctorSchedule>();
		list = officedao.showschedule(doctor_id);
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("schedulelist", list);
		ArrayList<Object> enableDay = new ArrayList<Object>();
		for(int i =0;i<list.size();i++){
			enableDay.add(list.get(i).getSdate());
		}
		mav.addObject("enableDay", enableDay);
		return mav;
	}
	
	@RequestMapping("changefloor.do")
	@ResponseBody
	public HashMap<String, Object> changeFloor(String floornum) throws ClassNotFoundException, SQLException{
		int floor = Integer.parseInt(floornum);
		OfficeDao officeDao = sqlSessionTemplate.getMapper(OfficeDao.class);
		System.out.println("floorNum : " + floor);
		List<Ward> list = new ArrayList<Ward>();
		list = officeDao.selectFloor(floor);
		System.out.println("selectFloor list.size : " + list.size());
		/*for(int i = 0; i < list.size(); i++){
			System.out.println("list.get(i).getWard_num() : " + list.get(i).getWard_num());
		}*/
		HashMap<String, Object> mapJson = new HashMap<String, Object>();
		mapJson.put("floorlist", list);
		
		List<WardMaster> list2 = new ArrayList<WardMaster>();
		list2 = officeDao.isEmpty();
		System.out.println("isEmpty list.size() : " + list.size());

		mapJson.put("isEmptylist", list2);

		return mapJson;
	}

	@RequestMapping("selectschedule.do")
	@ResponseBody
	public HashMap<String, Object> selectday(String sdate,String doctor_id) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		System.out.println(sdate);
		//System.out.println("//" + doctor_id);
		List<DoctorSchedule> list = new ArrayList<DoctorSchedule>();
		list = officedao.selectday(sdate,doctor_id);
		HashMap<String, Object> mapJson = new HashMap<String, Object>();
		mapJson.put("timelist", list);
		return mapJson;
	}
	
	@RequestMapping("deptschedule.do")
	@ResponseBody
	public ArrayList<Object> deptschedule(String url) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		List<CalBean> callist = new ArrayList<CalBean>();
		ArrayList<Object> ja = new ArrayList<Object>();
		callist = officedao.deptschedule(url);
		for(int i =0;i<callist.size();i++){
			CalBean calbean =callist.get(i);
			ja.add(calbean);
		}
		return ja;
		
	}
	
	@RequestMapping("addschedule.do")
	@ResponseBody
	public String addschedule(DoctorSchedule doctorschedule) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		System.out.println(doctorschedule.getSdate());
		int i = officedao.addschedule(doctorschedule);
		if(i>0){
			return "success";
		}else{
			return "fail";
		}
	}
	@RequestMapping("delschedule.do")
	@ResponseBody
	public String delschedule(DoctorSchedule doctorschedule) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		System.out.println(doctorschedule.getDOCTOR_ID());
		int i = officedao.delschedule(doctorschedule);
		if(i>0){
			return "success";
		}else{
			return "fail";
		}
	}
	
	@Transactional
	@RequestMapping("addreser.do")
	public String addreser(Jupsoo jupsoo) throws ClassNotFoundException, SQLException{
		String patient_id = jupsoo.getPatient_id();
		String schedule_id = jupsoo.getSchedule_id();
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		officedao.addreser_v(jupsoo);
		String visitant_id = officedao.visitant(patient_id);
		officedao.addreser_j(jupsoo);
		officedao.addwaiting(visitant_id);
		officedao.updateschedule(schedule_id);
		return "redirect:/patientinfo.do";
	}
	
	@Transactional
	@RequestMapping("addjupsoo.do")
	public String addjupsoo(Jupsoo jupsoo) throws ClassNotFoundException, SQLException{
		String patient_id = jupsoo.getPatient_id();
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		officedao.addreser_v(jupsoo);
		String visitant_id = officedao.visitant(patient_id);
		officedao.addreser_j2(jupsoo);
		officedao.addwaiting(visitant_id);
		return "redirect:/patientinfo.do";
	}
	
	@RequestMapping("wardPatientInfo.do")
	@ResponseBody
	public HashMap<String, Object> wardPatientInfo(String wardNum, String bedNum) throws ClassNotFoundException, SQLException{
		System.out.println("wardPatientInfo.do 컨트롤러");
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		
		int ward_Num = Integer.parseInt(wardNum);
		int bed_Num = Integer.parseInt(bedNum);
		
		System.out.println("wardNum : " + wardNum);
		System.out.println("bedNum : " + bedNum);
		
		List<WardMaster> wm = new ArrayList<WardMaster>(); 
		wm = (List<WardMaster>)officedao.wardPatientInfo(ward_Num, bed_Num);

		HashMap<String, Object> mapjson = new HashMap<String, Object>();
		mapjson.put("wardPatient", wm);
		
		return mapjson;
	}
	
	@RequestMapping("selecttime.do")
	@ResponseBody
	public HashMap<String, Object> selecttime(String doctor_id,String sdate) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		List<DoctorSchedule> list = new ArrayList<DoctorSchedule>();
		list = officedao.selectday2(sdate,doctor_id);
		HashMap<String, Object> mapJson = new HashMap<String, Object>();
		mapJson.put("timelist", list);
		return mapJson;
	}
	
	@RequestMapping("addjindan.do")
	public String addjindan(Jupsoo jupsoo) throws ClassNotFoundException, SQLException{
		String patient_id = jupsoo.getPatient_id();
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		officedao.addreser_v(jupsoo);
		officedao.addreser_j3(jupsoo);
		Jupsoo jupsooinfo = officedao.jupsoo_id(patient_id);
		String jupsoo_id = jupsooinfo.getJupsoo_id();
		return "redirect:/jindanjupsoo.do?jupsoo_id="+jupsoo_id+"&patient_id="+patient_id;
	}
	
	@RequestMapping("jindanjupsooInsert.do")
	public ModelAndView jindanjupsooinsert(
			String is_personally, String is_document, String target,
			  String jumin1,String jumin2, String jindantype,
			  String jupsoo_id, ModelAndView mav){
		
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		int i = officedao.jindanjupsooinsert(is_personally, is_document, target, jumin1, jumin2, jindantype, jupsoo_id);
		System.out.println("jindanjupsooInsert 성공!");
		
		if(i > 0 ){
			
			Jupsoo a = officedao.jupsoo_select(jupsoo_id);	 //접수Table의 jupsooid와 jindanjupsoo의 id가 같으면
			mav.addObject("a", a);
			System.out.println("select 성공!");
			
			
				if(a.getJupsoo_id() != null){
					System.out.println("이프문 들어옴");
					String visitant_id = a.getVisitant_id();
					String jupsoo_time = a.getJupsoo_time();
					String jupsoo_date = a.getJupsoo_date();
					String doctor_id = a.getDoctor_id();
					String dept_id = a.getDept_id();
					String jupsoo_id2 = a.getJupsoo_id();
					
					System.out.println(visitant_id+" / "+jupsoo_date+ " / "  +jupsoo_time+" / "+ doctor_id + " / " + dept_id + " / " + jupsoo_id2);
					officedao.cheobangwaiting_insert(jupsoo_id2);
				}	
			}
		mav.setViewName("redirect:/patientinfo.do");
		
				return mav;
	}
	
	@RequestMapping("cheobangdata.do")
	@ResponseBody
	public HashMap<String, Object> cheobangdata(String cheobang_id) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		String CHEOBANG_ID = cheobang_id;
		Cheobang_All cheobangall = officedao.cheobang_all3(cheobang_id);
		List<Medicine_Cheobang_Patient> list=officedao.medic_cheobang_p(CHEOBANG_ID);
		List<Medicine_Cheobang_Patient> doselist=officedao.dose(CHEOBANG_ID);
		HashMap<String, Object> maplist = new HashMap<String, Object>();
		maplist.put("list", list);
		maplist.put("doselist", doselist);
		maplist.put("cheobanglist", cheobangall);
		return maplist;
	}
	
	@RequestMapping("compareres.do")
	@ResponseBody
	public String compareTime(String res_time, String doctor_id,String sdate) throws ClassNotFoundException, SQLException{
		System.out.println("컨트롤러 : " + res_time + " / " + doctor_id);
		String resok = "ok";
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		List<DoctorSchedule> list = new ArrayList<DoctorSchedule>();
		
		list = officedao.selecttime(doctor_id, res_time,sdate);
		
		if(list.size() > 0){
			resok = "no";
		}
		
		
		return resok;
}
/*	
	@RequestMapping("death_jindan.do")
	public String death_jindan(){
		
	}*/
	
	
	//정보 컨트롤러
	@RequestMapping("doctorinsert.do")
	public ModelAndView doctorinsert(ModelAndView mav) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		mav.setViewName("office.doctorinsert");	
		return mav;
	}
	
	//정보 컨트롤러
	@RequestMapping("doctor_insert.do")
	@Transactional
	public ModelAndView doctor_insert(Doctor doctor,HttpServletRequest request) throws ClassNotFoundException, SQLException, IOException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		System.out.println("getfilename = " + doctor.getPhoto());
		
		String photo = doctor.getPhoto().substring(12);
		System.out.println(photo);
		doctor.setPhoto(photo);
		
		int i=officedao.doctor_insert(doctor);
		if(i >0){
			Doctor doctor2=officedao.doctor_select(doctor);
			List<Doctor> doctor3=officedao.dept_select();
			mav.addObject("doctor",doctor2);
			mav.addObject("dept",doctor3);
			return mav;
		}
		return mav;
	}
	
	
	@RequestMapping("deptdetail.do")
	@Transactional
	public ModelAndView deptdatil(String dept_id) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		List<Doctor> list=officedao.dept_detail_select(dept_id);
		mav.addObject("dept",list);	
		return mav;
	}
	@RequestMapping("deptdetail2.do")
	@Transactional
	public ModelAndView deptdetail2(String dept_detail_id,String doctor_id) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		int i=officedao.insert_major(doctor_id, dept_detail_id);
		if(i>0){
			mav.addObject("yes",i);
			return mav;
		}else{
			officedao.major_faile(doctor_id);
			officedao.doctor_faile(doctor_id);
			mav.addObject("no",i);
			return mav;	
		}

	}
	
	@RequestMapping("doctorinsertfinal.do")
	public ModelAndView doctorinsertfinal (String content,String start_year,String end_year,String doctor_id) throws ClassNotFoundException, SQLException{
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		int i=officedao.insert_doctor_info(content, start_year, end_year, doctor_id);
		if(i>0){
			mav.addObject("yes",i);
			return mav;
		}else{
			officedao.doctor_faile(doctor_id);
			mav.addObject("no",i);
			return mav;
			
		}
		
	}
	
	
	
}	